import java.sql.*;

/**
 * @author wq3stone
 */
public class DBUtil {
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection(){
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306?useSSL=false","root","123456");
            String sql = "create database if not exists calcSum";
            ps = conn.prepareStatement(sql);
            ps.execute();
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/calcSum?useSSL=false","root","123456");
            System.out.println("DBS is connected");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    public static void close(Connection conn, PreparedStatement ps, ResultSet rs){
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(ps!=null){
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        System.out.println("DBS is disconnected");
    }
}
